package com.cn.web.controller.mc;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.mc.*;
import com.cn.config.config.Result;
import com.cn.service.server.bc.MaterialServer;
import com.cn.service.server.mc.HcxxService;
import com.cn.service.server.mc.K3WlXxSQServer;
import com.cn.service.server.mc.McYcService;
import com.cn.service.server.mc.MoXiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
public class HcxxController {
    private final HcxxService hcxxService;
    private final MaterialServer materialServer;
    @Autowired(required = false)
    private MoXiaoService moXiaoService;
    @Autowired(required = false)
    private McYcService mcYcService;
    @Autowired(required = false)
    private K3WlXxSQServer k3WlXxSQServer;

    @Autowired(required = false)
    public HcxxController(HcxxService hcxxService, MaterialServer materialServer) {
        this.hcxxService = hcxxService;
        this.materialServer = materialServer;
    }

    @RequestMapping("/select/hcxx")
    @ResponseBody
    public List<Hcxx> selectHcxx(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String djBh = jsonObject.getString("djBh");
        String k3WlBm = jsonObject.getString("k3WlBm");
        djBh = (djBh != null) ? djBh : "";
        k3WlBm = (k3WlBm != null) ? k3WlBm : "";
        List<Hcxx> hcxxes = hcxxService.selectHcxx(limit, page, djBh, k3WlBm);
        return hcxxes;
    }

    @RequestMapping("/add/hcxx")
    @ResponseBody
    public Result addHcxx(@RequestBody JSONObject jsonObject) {
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String hcBh = jsonObject.getString("hcBh");
        String hcMc = jsonObject.getString("hcMc");
        BigDecimal gg = jsonObject.getBigDecimal("gg");
        String sl = jsonObject.getString("sl");
        String dw = jsonObject.getString("dw");
        String bz = jsonObject.getString("bz");
        String djBh = jsonObject.getString("djBh");
        String gzmDcMxcs = jsonObject.getString("gzmDcMxcs");
        String rs = jsonObject.getString("rs");
        String hysj = jsonObject.getString("hysj");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        int addhc = hcxxService.addHcxx(hcBh, hcMc, gg, sl, dw, bz, djBh, gzmDcMxcs, rs, hysj, userName, name, bmmc, currentFormattedDateTime);
        if (addhc == 1) {
            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");
    }

    @RequestMapping("/del/hcxx")
    @ResponseBody
    public Result delHcxx(@RequestBody JSONObject jsonObject) {
        String hcBh = jsonObject.getString("hcBh");
        int hc = hcxxService.delHcxx(hcBh);
        if (hc == 1) {
            return Result.success("200", "删除成功");
        }
        return Result.failure("500", "删除失败");
    }

    @RequestMapping("/update/hcxx")
    @ResponseBody
    public Result updateHcxx(@RequestBody JSONObject jsonObject) {
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String hcBh = jsonObject.getString("hcBh");
        String hcMc = jsonObject.getString("hcMc");
        BigDecimal gg = jsonObject.getBigDecimal("gg");
        String sl = jsonObject.getString("sl");
        String dw = jsonObject.getString("dw");
        String bz = jsonObject.getString("bz");
        String djBh = jsonObject.getString("djBh");
        String gzmDcMxcs = jsonObject.getString("gzmDcMxcs");
        String rs = jsonObject.getString("rs");

        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        int update = hcxxService.updateHcxx(hcBh, hcMc, gg, sl, dw, bz, djBh, gzmDcMxcs, rs, userName, name, bmmc, currentFormattedDateTime);
        if (update == 1) {

            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");
    }

    @RequestMapping("/insert/McHy")
    @ResponseBody
    public Result insertMcHy(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String gx = jsonObject.getString("gx");
        String sb = jsonObject.getString("sb");
        String banZu = jsonObject.getString("banZu");
        String bz = jsonObject.getString("bz");
        Integer zt = jsonObject.getInteger("zt");
        Integer id = hcxxService.getMcHcAutoid();
        if (id != null) {
            id++;
        } else {
            id = 1;
        }
        String documentNumber = String.valueOf(id);
        int k = hcxxService.addMcHc(userName, name, bmmc, currentFormattedDateTime, gx, sb, banZu, bz, id, documentNumber, currentFormattedDateTime, zt, 0);
        JSONArray formDataList = jsonObject.getJSONArray("formDataList");
        boolean isOk = true;
        for (int i = 0; i < formDataList.size(); i++) {
            JSONObject formData = formDataList.getJSONObject(i);
            McHcList hcxx = formData.toJavaObject(McHcList.class);
            Integer idList = hcxxService.getMcHcListAutoid();
            if (idList != null) {
                idList++;
            } else {
                idList = 1;
            }
            hcxx.setIdList(idList);
            hcxx.setParentId(id);
            int k1 = hcxxService.addMcHcList(hcxx);
            int k2 = hcxxService.upDateK3FlXxZt(hcxx.getDjBh());
            if (k1 == 0 || k2 == 0) {
                isOk = false;
            }
        }
        if (isOk) {
            return Result.success("200", "保存成功");
        }
        return Result.failure("500", "保存失败");
    }

    @RequestMapping("/MC/get/hcxx")
    @ResponseBody
    public List<K3FlXx> getK3FlXx(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String djBh = jsonObject.getString("djBh");
        String k3Mc = jsonObject.getString("k3Mc");
        String parentId = jsonObject.getString("parentId");
        Integer zt = jsonObject.getInteger("zt");
        djBh = (djBh != null) ? djBh : "";
        k3Mc = (k3Mc != null) ? k3Mc : "";
        parentId = (parentId != null) ? parentId : "";
        List<K3FlXx> k3FlXx = hcxxService.getK3FlXx(limit, page, djBh, k3Mc, parentId, zt);
        return k3FlXx;
    }

    //   主子表
    @RequestMapping("/select/Hao/Cai")
    @ResponseBody
    public List<McHc> SelectHaoCai(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String documentNumber = jsonObject.getString("documentNumber");
        String gx = jsonObject.getString("gx");
        Integer zt = jsonObject.getInteger("zt");
        documentNumber = (documentNumber != null) ? documentNumber : "";
        gx = (gx != null) ? gx : "";
        List<McHc> mcList = hcxxService.getMcMxList(limit, page, documentNumber, gx, zt);
        return mcList;
    }

    @RequestMapping("/get/McHc/List")
    @ResponseBody
    public List<McHcList> getMcHcList(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        List<McHcList> mcHcList = hcxxService.getMcHcList(id);
        return mcHcList;
    }

    //修改子表
    @RequestMapping("/up/Date/Mc/Hc")
    @ResponseBody
    public Result upDateMcHc(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        Integer id = jsonObject.getInteger("id");
        String documentNumber = jsonObject.getString("documentNumber");
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        String gx = jsonObject.getString("gx");
        String sb = jsonObject.getString("sb");
        String banZu = jsonObject.getString("banZu");
        String bz = jsonObject.getString("bz");
        Boolean isOK = true;
        int k = hcxxService.upDateMcHc(documentNumber, userName, name, bmmc, currentFormattedDateTime, gx, sb, banZu, bz);
        List<McHcList> mcHcList = hcxxService.getMcHcList(id);
        for (McHcList mcHcList1 : mcHcList) {
            int i = mcYcService.upDateMcK3FlDgXxZt(mcHcList1.getDjBh(), 7);
        }
        JSONArray formDataList = jsonObject.getJSONArray("formDataList");
        if (!formDataList.isEmpty()) {
            int i1 = hcxxService.deleteMcHcList(id);
            for (int i = 0; i < formDataList.size(); i++) {
                JSONObject formData = formDataList.getJSONObject(i);
                McHcList hcList = formData.toJavaObject(McHcList.class);
                Integer mcHcListAutoid = hcxxService.getMcHcListAutoid();
                if (mcHcListAutoid != null) {
                    mcHcListAutoid++;
                } else {
                    mcHcListAutoid = 1;
                }
                hcList.setParentId(id);
                hcList.setIdList(mcHcListAutoid);
                int i2 = hcxxService.addMcHcList(hcList);
                if (i2 == 0) {
                    isOK = false;
                }
            }
        }
        if (isOK) {
            return Result.success("200", "保存成功");
        }
        return Result.failure("500", "保存失败");

    }

    @RequestMapping("/up/Date/McHc/Sh")
    @ResponseBody
    public Result upDateMcHcSh(@RequestBody JSONObject jsonObject) {
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        JSONArray idArray = jsonObject.getJSONArray("id");
        Integer zt = jsonObject.getInteger("zt");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        boolean isOk = true;
        if (!idArray.isEmpty()) {
            for (Object currentIdObj : idArray) {
                int currentId = (Integer) currentIdObj;
                int i = hcxxService.upDateMcHcSh(currentId, zt, 1, userName, name, bmmc, currentFormattedDateTime);
                List<McHcList> mcHcList = hcxxService.getMcHcList(currentId);
                for (McHcList mcList : mcHcList) {
                    int i1 = hcxxService.upDateK3FlXxZt(mcList.getDjBh());
                    if (i1 == 0 || i == 0) {
                        isOk = false;
                    }
                }
            }
        }
        if (isOk) {
            return Result.success("200", "保存成功");

        }
        return Result.failure("500", "保存失败");

    }

    @RequestMapping("/up/Date/McHc/Sh/QS")
    @ResponseBody
    public Result upDateMcHcShQs(@RequestBody JSONObject jsonObject) {
        String currentFormattedDateTime = jsonObject.getString("currentFormattedDateTime");
        currentFormattedDateTime = currentFormattedDateTime.replace("/", "-");
        Integer zt = jsonObject.getInteger("zt");
        String userName = jsonObject.getString("userName");
        List<User> users = materialServer.selectUser(userName);
        String bmmc = users.get(0).getBmmc();
        String name = users.get(0).getName();
        JSONArray id = jsonObject.getJSONArray("id");
        if (!id.isEmpty()) {
            for (int i = 0; i < id.size(); i++) {
                int d = (int) id.get(i);
                List<McHcList> mcHcList = hcxxService.getMcHcList(d);
                for (McHcList mcHcList1 : mcHcList) {
                    String djBh = mcHcList1.getDjBh();
                    int gzmDcMxcs = mcHcList1.getGzmDcMxcs();
//                    K3FlXx k3FlXx = k3WlXxSQServer.selectK3FlXxDjBh(djBh);
//                    int gzmDcMxcs = k3FlXx.getGzmDcMxcs();
                    Integer cite = moXiaoService.isCite(gzmDcMxcs+1, djBh);
                    if (cite == null || cite == 0) {
                        int i1 = mcYcService.upDateMcK3FlDgXxZt(djBh, zt);
                        int i2 = hcxxService.upDateMcHcSh(d, zt, 0, "", "", "", "");

                    }else {
                        return Result.failure("500", "单据中的辊号已被引用，无法弃审");
                    }

                }
            }
        }


        return Result.success("200", "保存成功");


    }
}
